<template>
  <XtxDialog :title="`${fromDate.id ? '修改' : '添加'}收货地址`" v-model:visible="visibleDialog">
    <div class="address-edit">
      <div class="xtx-form">
        <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input v-model="fromDate.receiver" class="input" placeholder="请输入收货人" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">手机号：</div>
          <div class="field">
            <input v-model="fromDate.contact" class="input" placeholder="请输入手机号" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地区：</div>
          <div class="field">
            <XtxCity :fullLocation="fromDate.fullLocation" @change="changeCity" placeholder="请选择收货地址" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">详细地址：</div>
          <div class="field">
            <input v-model="fromDate.address" class="input" placeholder="请输入详细地址" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">邮政编码：</div>
          <div class="field">
            <input v-model="fromDate.postalCode" class="input" placeholder="请输入邮政编码" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地址标签：</div>
          <div class="field">
            <input v-model="fromDate.addressTags" class="input" placeholder="请输入地址标签，逗号分隔" />
          </div>
        </div>
      </div>
    </div>
    <template #footer>
      <XtxButton @click="visibleDialog = false" type="gray" style="margin-right:20px">取消</XtxButton>
      <XtxButton @click="submit" type="primary">确认</XtxButton>
    </template>
  </XtxDialog>
</template>

<script>
import { ref, reactive } from 'vue'
import { addAddress,editAddress } from '@/api/order'
import Message from '@/components/library/Message'
export default {
  name: 'AddressEdit',
  setup(props, { emit }) {
    //控制AddressEdit组件的显示和隐藏
    const visibleDialog = ref(false)

    //打开AddressEdit的方法
    const open = (address) => {
      visibleDialog.value = true

      if (address.id) {
        //如果传过来有值进行修改 填充表格
        for (const key in address) {
          fromDate[key] = address[key]
        }

      } else {
        //没有值 清空数据
        for (const key in fromDate) {
          if (key === 'isDefault') {
            fromDate[key] = 1
          } else {
            fromDate[key] = null
          }
        }
      }

    }

    //定义填写表单数据对象
    const fromDate = reactive({
      receiver: null,  //收货人姓名
      contact: null,   //联系方式
      provinceCode: null,  //所在省份编码
      cityCode: null,  //所在城市编码
      countyCode: null,  //所在区/县编码
      address: null,  //详细地址
      postalCode: null,  //邮政编码
      addressTags: null,  //地址标签,以英文逗号分割
      isDefault: 1,  //是否为默认，0为是，1为否
      fullLocation: null //完整地址
    })

    //通过自定义事件获取xtx-city组件传递过来的数据
    const changeCity = (result) => {
      fromDate.provinceCode = result.provinceCode
      fromDate.cityCode = result.cityCode
      fromDate.countyCode = result.countyCode
      fromDate.fullLocation = result.fullLocation
    }

    //提交按钮
    const submit = () => {
      if (fromDate.id) {
        //修改地址请求
        editAddress(fromDate).then(data => {
           // 修改成功时的提示
          Message({ type: 'success', text: '修改地址成功' })
          //关闭对话框
          visibleDialog.value = false
          //触发自定义事件  （修改/添加）
          emit('on-success', fromDate)
        })
      } else {
        //添加地址请求
        addAddress(fromDate).then(data => {
          //设置id
          fromDate.id = data.result.id
          // 添加成功时的提示
          Message({ type: 'success', text: '添加地址成功' })
          //关闭对话框
          visibleDialog.value = false
          //触发自定义事件  （修改/添加）
          emit('on-success', fromDate)

        }, (e) => {
          Message({ text: '添加地址失败' })
          visibleDialog.value = false
        })
      }

    }

    return {
      visibleDialog,
      open,
      fromDate,
      changeCity,
      submit
    }
  }
}
</script>

<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper) {
    width: 780px;

    .body {
      font-size: 14px;
    }
  }
}

.xtx-form {
  padding: 0;

  input {
    outline: none;

    &::placeholder {
      color: #ccc;
    }
  }
}

.xtx-city {
  width: 320px;

  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;

    .placeholder {
      color: #ccc;
    }

    i {
      color: #ccc;
      font-size: 18px;
    }

    .value {
      font-size: 14px;
    }
  }

  :deep(.option) {
    top: 49px;
  }
}
</style>